<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Models\Admin\TotalBonusPool;
use App\Models\Admin\User;
use App\Models\Admin\DailyAmount;
use Log;
use Carbon\Carbon;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        $schedule->call(function () {
            $this->AutoEveryDayGold();
        })->dailyAt('00:00');//每天零点执行
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

    private function AutoEveryDayGold(){
        $store_id = 3;
        $pool = new TotalBonusPool;
        $bonus = $pool->where('store_id',$store_id)->first();
        //公司账号余额
        $user = new User;
        $company = $user->where('id',1)->first();
        $memberGold = $user->where('id','!=',1)->sum('gold');
        
        $data = [];
        $info = [];
        $arr = [];
        $arr['store_id'] = $store_id;
        $arr['gold'] = $company->gold;
        $arr['bonus_pool_one'] = $bonus->bonus_pool_one_sum;
        $arr['team_month'] = $bonus->team_month_sum;
        $arr['team_year'] = $bonus->team_year_sum;
        $arr['team_recommend'] = $bonus->team_recommend_sum;
        $arr['team_fund'] = $bonus->team_fund_sum;
        $arr['member_gold'] = $memberGold;
        $arr['created_time'] = Carbon::now()->toDateTimeString();

        $result = DailyAmount::insert($arr);
        if($result){
            Log::info('每日金额存储成功');
        }else{
            Log::info('每日金额存储失败'.$arr['gold'].'虹包:'.$arr['bonus_pool_one'].'报单奖励:'.$arr['team_month'].'分虹:'.$arr['team_year'].'招商奖励:'.$arr['team_recommend'].'教育基金:'.$arr['team_fund'].'用户总余额:'.$arr['member_gold']);
        }
    }
}
